﻿
using System.Text;
using FirebirdSql.Data.FirebirdClient;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            FbConnectionStringBuilder connectionStringBuilder = new FbConnectionStringBuilder();
            connectionStringBuilder.UserID = "sysdba";
            connectionStringBuilder.ServerType = FbServerType.Embedded;
            if (args.Length > 0)
            {
                if (args[0] == "1")
                {
                    connectionStringBuilder.Database = "D:\\tmhczcz12\\Train.FDB";
                }
                else
                {
                    connectionStringBuilder.Database = "D:\\tmhczcz22\\Train.FDB";
                }
            }
            //connectionStringBuilder.Database = "D:\\tmhczcz12\\Train.FDB";
            using (FbConnection fbConn = new FbConnection(connectionStringBuilder.ConnectionString))
            {
                fbConn.Open();
                using (FbCommand selectData = fbConn.CreateCommand())
                {
                    string key = "";
                    selectData.CommandText = "select CCXX_KEY, CCXX_CCBH, CCXX_CCSJ, CCXX_START, CCXX_END, CCXX_ZCPS, CCXX_ZEDZ, CCXX_ZSJM, CCXX_ZWCS, CCXX_XM01, MQSJC from CCXX ORDER BY MQSJC DESC ROWS 1";
                    using (FbDataReader r = selectData.ExecuteReader())
                    {
                        while (r.Read())
                        {
                            key = r.GetString(0);
                            string strTemp = "";
                            for(int i = 0 ; i < r.FieldCount; i++)
                            {
                                strTemp += "|" + r.GetValue(i);
                            }
                            Console.WriteLine(strTemp);
                        }
                    }
                    selectData.CommandText = "select CPXX_KEY,CPXX_PZR,CPXX_CXLX,CPXX_ID,CPXX_CPXH,CPXX_CPBH,CPXX_EDZZ,CPXX_SJZZ,CPXX_ZCWC,CPXX_ZCSJ,MQSJC from CPXX where CPXX_CCKEY = '" + key + "' ORDER BY CPXX_ID";
                    using (FbDataReader r2 = selectData.ExecuteReader())
                    {
                        while (r2.Read())
                        {
                            string strTemp = "";
                            for (int i = 0; i < r2.FieldCount; i++)
                            {
                                strTemp += "|" + r2.GetValue(i);
                            }
                            Console.WriteLine(strTemp);
                        }
                    }
                }
                fbConn.Close();
            }
        }
    }
}